package cn.com.bluemoon.daps.system.controller;


import cn.com.bluemoon.daps.common.datascope.BmPermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.enums.DatabaseType;
import cn.com.bluemoon.daps.system.entity.DapSystemDatasource;
import cn.com.bluemoon.daps.system.entity.DapSystemSchema;
import cn.com.bluemoon.daps.system.service.DapSystemSchemaService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "schema管理")
@RestController
@Slf4j
@RequestMapping("/dapSystem")
public class DapSystemSchemaController {

    @Autowired
    DapSystemSchemaService dapSystemSchemaService;

    @BmPermission(value = "system:schema:list", name = "schema列表查询", ignore = true)
    @GetMapping("schema/all")
    @ApiOperation(value = "schema列表")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = DapSystemSchema.class)})
    public ResultBean<IPage<DapSystemSchema>> getSchema(@ApiParam(value = "每页显示的数量", example = "0") @RequestParam Integer pageSize,
                                                            @ApiParam(value = "当前页码数", example = "0") @RequestParam Integer pageNum,
                                                            @ApiParam("数据源名称") @RequestParam(required = false) String schemaName){

        return ResultBean.ok(dapSystemSchemaService.getSchemaPage(pageSize, pageNum, schemaName));
    }

    @BmPermission(value = "system:schema:add", name = "新增schema", ignore = true)
    @PostMapping("add/schema")
    @ApiOperation(value = "新增schema")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = DapSystemSchema.class)})
    public ResultBean<String> addSchema(@RequestBody @Valid DapSystemSchema schema){

        return dapSystemSchemaService.insertSchema(schema);
    }

    @BmPermission(value = "system:schema:add", name = "删除schema", ignore = true)
    @DeleteMapping("delete/schema")
    @ApiOperation(value = "删除schema")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = DapSystemSchema.class)})
    public ResultBean<String> deleteSchema(@ApiParam("id") @RequestBody @Valid List<String> ids){

        dapSystemSchemaService.getBaseMapper().deleteBatchIds(ids);
        return ResultBean.ok();
    }

}
